package com.hangim.pce.service.impl;

import com.hangim.pce.enums.DoseUnit;
import com.hangim.pce.enums.TimeUnit;
import com.hangim.pce.enums.UsageType;
import com.hangim.pce.mapper.SysPrescriptionDetailMapper;
import com.hangim.pce.model.SysPrescription;
import com.hangim.pce.model.SysPrescriptionDetail;
import com.hangim.pce.model.vo.request.DrugListVo;
import com.hangim.pce.service.SysPrescriptionDetailService;
import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 处方明细表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class SysPrescriptionDetailServiceImpl extends BaseEntityServiceImpl<SysPrescriptionDetailMapper, SysPrescriptionDetail> implements SysPrescriptionDetailService {

    @Override
    public void savePrescriptionDetail(List<DrugListVo> drugList, SysPrescription prescription) {
        for (DrugListVo vo : drugList) {
            SysPrescriptionDetail detail = new SysPrescriptionDetail();
            detail.setPresId(prescription.getId());
            detail.setDrugId(vo.getDrugId());
            detail.setPack(vo.getNorms());
            detail.setName(vo.getDrugName());
            detail.setDose(Integer.parseInt(vo.getDose()));
            detail.setUnit(DoseUnit.getDoseUnitByDesc(vo.getDoseUnit()).getNum());
            detail.setFreq(Integer.parseInt(vo.getFreq()));
            detail.setFreqUnit(TimeUnit.getTimeUnitByDesc(vo.getFreqUnit()).getNum());
            detail.setUsage(UsageType.getUsageTypeByDesc(vo.getUseage()).getNum());
            detail.setCreatedDate(getTimestamp());
            detail.setActive(true);
            save(detail);
        }
    }
}
